package cn.huijielong.common.utils;
import java.io.*;  
import java.awt.*;
import java.awt.image.*;  
import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.*;
import cn.huijielong.common.exception.VSException;;
/** 
 * 图片压缩处理 
 */  
public class ImgCompress {  
    private Image img;  
    private int width;  
    private int height;  
   
    public static byte[] run(InputStream in){
    	try {
    		ImgCompress ic=new ImgCompress(in);
			return ic.resizeFixOri();
		} catch (IOException e) {
			throw new VSException("压缩文件出错",e);
		}
    }
    public static byte[] run(byte[] in) {
    	return run(in, 0);
    }
    public static byte[] run(byte[] in,int width){
    	try {
    		ImgCompress ic=new ImgCompress(new ByteArrayInputStream(in));
    		return ic.resizeByWidth(width);
    	} catch (IOException e) {
    		throw new VSException("压缩文件出错",e);
    	}
    }
    /** 
     * 构造函数 
     */  
    public ImgCompress(InputStream in) throws IOException {  
        img = ImageIO.read(in);      // 构造Image对象  
        width = img.getWidth(null);    // 得到源图宽  
        height = img.getHeight(null);  // 得到源图长  
    }  
    public byte[] resizeFixOri() throws IOException {
    	return resizeByWidth(width);
    }
 
    /** 
     * 以宽度为基准，等比例放缩图片 
     * @param w int 新宽度 
     */  
    public byte[] resizeByWidth(int w) throws IOException {
    	int h=0;
    	if(w>=width || w<=0)
    	{
    		w=width;
    		h = height;
    	}else {
    		h = (int) (height * w / width);
    	}
        return resize(w, h);  
    }  
 
    /** 
     * 强制压缩/放大图片到固定的大小 
     * @param w int 新宽度 
     * @param h int 新高度 
     */  
    public byte[] resize(int w, int h) throws IOException {  
        // SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢  
        BufferedImage image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB );   
        image.getGraphics().drawImage(img.getScaledInstance( w, h,Image.SCALE_SMOOTH), 0, 0, null); // 绘制缩小后的图  
        ByteArrayOutputStream out = new ByteArrayOutputStream(); // 输出
        // 可以正常实现bmp、png、gif转jpg  
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
        encoder.encode(image); // JPEG编码  
        out.close();
        return out.toByteArray();
    }  
    
    public static void main(String[] args) {
		try {
			byte[] bs=org.apache.commons.io.FileUtils.readFileToByteArray(new File("F:\\temp\\大图\\1234.jpg"));
			byte[] ws=ImgCompress.run(bs,2000);
			org.apache.commons.io.FileUtils.writeByteArrayToFile(new File("F:\\temp\\大图\\ws.jpg"), ws);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
    
    public static void createThumbnail(String src, String dist, float width,
            float height) {
        try {
            File srcfile = new File(src);
            if (!srcfile.exists()) {
                System.out.println("文件不存在");
                return;
            }
            BufferedImage image = ImageIO.read(srcfile);

            // 获得缩放的比例
            double ratio = 1.0;
            // 判断如果高、宽都不大于设定值，则不处理
            if (image.getHeight() > height || image.getWidth() > width) {
                if (image.getHeight() > image.getWidth()) {
                    ratio = height / image.getHeight();
                } else {
                    ratio = width / image.getWidth();
                }
            }
            // 计算新的图面宽度和高度
            int newWidth = (int) (image.getWidth() * ratio);
            int newHeight = (int) (image.getHeight() * ratio);

            BufferedImage bfImage = new BufferedImage(newWidth, newHeight,
                    BufferedImage.TYPE_INT_RGB);
            bfImage.getGraphics().drawImage(
                    image.getScaledInstance(newWidth, newHeight,
                            Image.SCALE_SMOOTH), 0, 0, null);

            FileOutputStream os = new FileOutputStream(dist);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
            encoder.encode(bfImage);
            os.close();
            System.out.println("创建缩略图成功");
        } catch (Exception e) {
            System.out.println("创建缩略图发生异常" + e.getMessage());
        }
    }
}  